<template>
  <div class="infoConfig">
    <el-form ref="form" :model="form" label-width="120px" :rules="rules">
      <AddList v-model="baseData" :show-add-btn="false" :show-del-btn="false">
        <template v-slot="{item: { item, index }}">
          <el-form-item :label="item.agentTypeZh" class="listItem" :prop="'line_'+index">
            <div>
              流量余额&lt;  <el-input v-model="item.flowBalance" class="numInput" size="small" />时，通知：
              <el-input v-model="item.notice" type="textarea" :autosize="{ minRows: 3, maxRows: 6 }" />
            </div>
          </el-form-item>
        </template>
      </AddList>
      <div class="btnList textCenter">
        <el-button size="small" @click="$emit('close')">取消</el-button>
        <el-button type="primary" size="small" @click="submit">保存配置</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { flowUpAgt } from '@/api/flowConf'
import AddList from '@/components/AddList/index.vue'
export default {
  components: { AddList },
  props: {
    infoConfig: {}
  },
  data() {
    return {
      form: {},
      baseData: []
    }
  },
  computed: {
    pageType() { return this.infoConfig.type },
    rules() {
      const obj = {}
      this.baseData.forEach((item, index) => {
        obj['line_' + index] = [{ validator: this.valid, trigger: 'change' }]
      })
      return obj
    }
  },
  mounted() {
    this.baseData = JSON.parse(JSON.stringify(this.infoConfig.data))
  },
  methods: {
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          console.log('form', this.form)
          flowUpAgt(this.baseData).then(res => {
            this.$message.success('操作成功！')
            this.$emit('sucClose')
          })
        } else {
          this.$message.error('表单信息填写有误，请重新检查！')
          return false
        }
      })
    },
    valid(rule, value, callback) {
      const dataIndex = rule.field.split('_')[1]
      const data = this.baseData[dataIndex]
      let text = ''
      if (!data.flowBalance || !data.notice) {
        text = '请填写完整信息'
      }

      console.log(rule, data)
      text ? callback(text) : callback()
    },
    addLine(type) {
      switch (type) {
        case 'merc':
          this.mercList.push({ name: '一级代理', a: 123, b: 123 })
          break
        default:
          break
      }
    }
  }

}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.infoConfig{
  padding: 30px;
  .title{
    font-size: 15px;
    color: #000;
    margin: 30px 0;
  }
  .numInput{
    width: 100px;
    margin: 0 10px;
  }
  // .listItem{
  //   border: 1px solid rgba(255, 183, 47, 1);
  //   border-radius: 10px;
  //   padding: 10px;
  //   margin: 10px 0;
  //   .content{
  //     display: flex;
  //     align-items: center;
  //     div{
  //       flex: 1;
  //     }
  //   }
  // }
}
</style>
